package com.hrms.doctor.service.impl;

import com.hrms.patient.constant.Constants;
import com.hrms.patient.core.domain.AjaxResult;
import com.hrms.patient.core.domain.entity.SysUser;
import com.hrms.patient.core.domain.model.RegisterBody;
import com.hrms.patient.utils.SecurityUtils;
import com.hrms.doctor.domain.Doctor;
import com.hrms.doctor.domain.DoctorPatient;
import com.hrms.doctor.domain.vo.DoctorPatientVo;
import com.hrms.doctor.domain.vo.DoctorVo;
import com.hrms.doctor.mapper.DoctorMapper;
import com.hrms.doctor.mapper.ScheduleMapper;
import com.hrms.doctor.service.DoctorService;
import com.hrms.doctor.service.ScheduleService;
import com.hrms.framework.web.service.SysRegisterService;
import com.hrms.system.service.ISysUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

@Service

public class DoctorServiceImpl implements DoctorService {
    @Autowired
    DoctorMapper doctorMapper;
    @Autowired
    ScheduleService scheduleService;
    @Autowired
    ScheduleMapper scheduleMapper;
    @Autowired
    SysRegisterService registerService;
    @Autowired
    ISysUserService sysUserService;

    @Override
    public List<Doctor> selectDoctor( String name, String phone){
        List<Doctor> list=doctorMapper.selectDoctor(name,phone);
       // PageHelper.startPage(page,pageSize);
        //PageBean pageBean=new PageBean(p.getTotal(),p.getResult());

        return list;
    }

    @Override

    public Integer insertDoctor(Doctor doctor){
        //启用
        doctor.setStatus((short)1);
        doctor.setCreateTime(LocalDateTime.now());

        return doctorMapper.insertDoctor(doctor);
    }
    @Override
    @Transactional
    public  Integer deleteById(List<Long> list){
        Long []userIds=list.toArray(new Long[list.size()]);
        sysUserService.deleteUserByIds(userIds);
        Integer rs=doctorMapper.deleteById(list);
        doctorMapper.deleteDoctorPatient(list);
        scheduleService.deleteScheduleById(list);
        return rs;
    }
    @Override
    public Doctor selectDoctorById(Long id){
        return doctorMapper.selectDoctorById(id);
    }
    @Override
    @Transactional
    public Integer update(Doctor doctor){

            SysUser sysUser=new SysUser();
            sysUser.setUserId(doctor.getId());

            sysUser.setStatus(doctor.getStatus()==0?"1":"0");
            sysUser.setPassword(SecurityUtils.encryptPassword(doctor.getPassword()));
            Long []userIds=new Long[1];
            userIds[0]=100L;
            sysUser.setRoleIds(userIds);
            sysUserService.updateUser(sysUser);


        Integer rs=doctorMapper.update(doctor);
        return rs;
    }


    @Override
    public Integer insertPatient(DoctorPatient doctorPatient){
        return doctorMapper.insertPatient(doctorPatient);

    }
    @Override
    public  List<DoctorPatientVo> selectDoctorPatient(Long id,String name, String phone){

        List<DoctorPatientVo>  list=doctorMapper.selectDoctorPatient(id,name, phone);
        list.forEach(p->{
            p.setCnt(scheduleService.selectCountByPatientId(p.getId()));
        });
        return list;
    }
    @Override
    public Integer deletePatientById(Long id,List<Long> list){
        return doctorMapper.deletePatientById(id,list);
    }
    @Override
    public void deleteDoctorPatient(List<Long> list){
        doctorMapper.deleteDoctorPatient(list);
    }

    @Override
    public List<DoctorVo> selectDoctorByName(String name,String deptName) {
       // String deptName="牛马";//TODO: 获取token
        List<DoctorVo> list=doctorMapper.selectDoctorByName(name,deptName);

        list.forEach(p->{
            p.setCnt(scheduleService.selectCountByDoctorId(p.getId()));

        });

    return list;
    }
}
